package com.wx.springboot.step2.controller;

import com.wx.springboot.step2.common.api.CommonPage;
import com.wx.springboot.step2.common.api.CommonResult;
import com.wx.springboot.step2.common.api.ResultCode;
import com.wx.springboot.step2.dto.PageDto;
import com.wx.springboot.step2.exception.ApiAssert;
import com.wx.springboot.step2.exception.ApiException;
import com.wx.springboot.step2.mbg.model.Brand;
import com.wx.springboot.step2.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 品牌管理
 */
@Api(tags = "品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 品牌列表
     * @return
     */
    @ApiOperation(value = "品牌列表")
    @GetMapping("/list")
    public CommonResult<List<Brand>> list () {
        List<Brand> brandList = brandService.list();
        return CommonResult.success(brandList);
    }

    /**
     * 品牌列表-分页
     * @return
     */
    @ApiOperation(value = "品牌列表-分页")
    @GetMapping("/list/page")
    public CommonResult<CommonPage<Brand>> listPage (PageDto pageDto) {
        List<Brand> brandList = brandService.list(pageDto);
        return CommonResult.success(CommonPage.restPage(brandList));
    }


    /**
     * 品牌详情
     * @return
     */
    @ApiOperation(value = "品牌需求")
    @GetMapping("/")
    public CommonResult<Brand> getBrand (@ApiParam("品牌id") @RequestParam  Long id) {
        if (id == 0) {
            ApiAssert.failed(ResultCode.VALIDATE_FAILED);
        }
        Brand brand = brandService.findById(id);
        return CommonResult.success(brand);
    }
}
